package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.pojo.TCheckgroup;
import com.qf.service.TCheckgroupService;
import com.qf.mapper.TCheckgroupMapper;
import com.qf.utils.Result;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
* @author Lenovo
* @description 针对表【t_checkgroup】的数据库操作Service实现
* @createDate 2024-07-17 22:31:51
*/
@Service
@RequiredArgsConstructor
public class TCheckgroupServiceImpl extends ServiceImpl<TCheckgroupMapper, TCheckgroup>
    implements TCheckgroupService{
   private final TCheckgroupMapper checkgroupMapper;
    @Override
    public Result findAllCheckgroup() {
//        List<TCheckgroup> tCheckgroups =
        return new Result(true,"查询成功",checkgroupMapper.selectList(null));
    }

    @Override
    public Result findCheckgroupPage(int page, int pageSize, String queryString) {
        LambdaQueryWrapper<TCheckgroup> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(queryString),TCheckgroup::getName,queryString)
                .or().like(StringUtils.hasText(queryString),TCheckgroup::getCode,queryString)
                .or().like(StringUtils.hasText(queryString),TCheckgroup::getHelpcode,queryString);
        Page<TCheckgroup> p = new Page<>(page, pageSize);
        Page<TCheckgroup> tCheckgroupPage = checkgroupMapper.selectPage(p, wrapper);
        return new Result(true,"查询成功",tCheckgroupPage);
    }

    @Override
    public Result findById(int id) {
        TCheckgroup tCheckgroup = checkgroupMapper.selectById(id);
        return new Result(true,"查询成功",tCheckgroup);
    }

    @Override
    public Result findCheckItemIdsByCheckGroupId(long id) {
        List<Integer> checkGroupId = checkgroupMapper.findCheckItemIdsByCheckGroupId(id);
        return new Result(true,"查询成功",checkGroupId);

    }

    @Override
    public Result edit(Integer[] checkitemIds, TCheckgroup checkgroup) {
        checkgroupMapper.updateById(checkgroup);
        checkgroupMapper.deleteCheckItemIdsByCheckGroupId(checkgroup.getId());
        checkgroupMapper.addCheckItemIdsByCheckGroupId(checkitemIds,checkgroup.getId());
        return new Result(true,"编辑成功");
    }

    @Override
    public Result add(Integer[] checkitemIds, TCheckgroup checkgroup) {
        checkgroupMapper.insert(checkgroup);
        checkgroupMapper.addCheckItemIdsByCheckGroupId(checkitemIds,checkgroup.getId());
        return new Result(true,"添加成功");
    }

    @Override
    public Result delete(long id) {
        //删除中间表
        checkgroupMapper.deleteCheckItemIdsByCheckGroupId((int)id);

        checkgroupMapper.deleteCheckItemIdsByCheckGroupId((int) id);
        //删除
        checkgroupMapper.deleteById(id);
        return new Result(true,"删除成功");
    }
}




